package com.prince.web.service.impl;

import com.prince.bean.CommonWebResponse;
import com.prince.bean.ResultParams;
import com.prince.bean.WebCode;
import com.prince.constant.Constants;
import com.prince.mapper.ProductMapper;
import com.prince.mapper.UnitMapper;
import com.prince.model.ProductExample;
import com.prince.model.Unit;
import com.prince.model.UnitExample;
import com.prince.page.Page;
import com.prince.util.DateUtil;
import com.prince.util.ListUtils;
import com.prince.util.MapUtil;
import com.prince.util.StringUtil;
import com.prince.web.service.UnitService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 计量单位
 * @Author: 王培喜 58528792@163.com
 * @Date: 2021/4/6 15:06
 */
@Log4j
@Service
public class UnitServiceImpl implements UnitService {

    @Resource
    private UnitMapper unitMapper;
    @Resource
    private ProductMapper productMapper;
    /**
     * 功能描述: 分页查询计量单位
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @Override
    public Map<String, Object> findUnitByPage(Map<String, Object> params, Page page) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        UnitExample unitExample = new UnitExample();
        UnitExample.Criteria criteria = unitExample.createCriteria().andDelEqualTo(Constants.YES);
        //封装条件
        if (MapUtil.isNotEmpty(params, "startTime")) {
            Date startTime = DateUtil.strToDate(params.get("startTime").toString(), "yyyy-MM-dd hh:mm:ss");
            Date endTime = DateUtil.strToDate(params.get("endTime").toString(), "yyyy-MM-dd hh:mm:ss");
            criteria.andCreatedBetween(startTime, endTime);
        }
        if(MapUtil.isNotEmpty(params,"name")){
            criteria.andNameLike("%"+params.get("name")+"%");
        }
        if(MapUtil.isNotEmpty(params,"pinyin")){
            criteria.andPinyinEqualTo(params.get("pinyin").toString());
        }

        //查询总数
        int total = unitMapper.countByExample(unitExample);
        if (null != page) {
            unitExample.setPage(page);
        }
        unitExample.setOrderByClause("created desc");
        List<Unit> list = unitMapper.selectByExample(unitExample);
        return response.toHashMapByPage(total,list);
    }
    /**
     * 功能描述:  添加计量单位
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:03
     */
    @Override
    public Map<String, Object> addUnit(Unit unit) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        //先判断是否存在 不存在再添加
        if(StringUtil.isEmpty(unit.getName())){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.parameterNull).toHashMap();
        }
        //校验名称是否存在
        boolean flag = checkTheName(unit.getName());
        if(flag){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.dataDuplication).toHashMap();
        }

        //添加
        unit.setUpdater(unit.getCreater());
        unit.setCreated(new Date());
        unit.setDel(Constants.YES);
        int i = unitMapper.insertSelective(unit);
        if(i < 1){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.dataDuplication).toHashMap();
        }
        return response.toHashMap();
    }

    /**
     * 功能描述: 校验名称
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:34
     */
    private boolean checkTheName(String name){
        if(StringUtil.isEmpty(name)){
            return true;
        }
        UnitExample unitExample = new UnitExample();
        UnitExample.Criteria criteria = unitExample.createCriteria().andDelEqualTo(Constants.YES);
        criteria.andNameEqualTo(name.trim());
        List<Unit> units = unitMapper.selectByExample(unitExample);
        if(ListUtils.isNotEmpty(units)){
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 修改计量单位
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> updateUnit(Unit unit) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        if (StringUtil.isNotEmpty(unit.getName()) && StringUtil.isNotEmpty(unit.getRemark())
                && StringUtil.stringNotEquals(unit.getName(),unit.getRemark())) {
            //校验
            boolean flag = checkTheName(unit.getName());
            if(flag){
                return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.dataDuplication).toHashMap();
            }
        }
        unit.setRemark(null);
        int i = unitMapper.updateByPrimaryKeySelective(unit);
        if(i < 1){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.updateError).toHashMap();
        }
        return response.toHashMap();
    }
    /**
     * 功能描述: 删除计量单位
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> delUnit(Unit unit) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        unit.setDel(Constants.NO);
        int i = unitMapper.updateByPrimaryKeySelective(unit);
        if(i < 1){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.delError).toHashMap();
        }
        return response.toHashMap();
    }
    /**
     * 功能描述: 通过id查询计量单位
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> findUnitById(Integer id) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        Unit unit = unitMapper.selectByPrimaryKey(id);
        response.put("data",unit);
        return response.toHashMap();
    }
    /**
     * 功能描述: 查询所有计量单位
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> findUnitAll() {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        UnitExample example = new UnitExample();
        example.createCriteria().andDelEqualTo(Constants.YES);
        List<Unit> list = unitMapper.selectByExample(example);
        list.forEach(x->{
            x.setRemark(x.getName());
        });
        response.put("data",list);
        return response.toHashMap();
    }

}
